from django.shortcuts import render
from django.http import HttpResponse,JsonResponse
from .. models import Goods,Types
from . typeviews import typetall
import os,time
from django.core.paginator import Paginator

def goodsadd(request):
    ob = typetall()
    context = {'typelist':ob}
    return render(request,'myadmin/goods/goodsadd.html',context)

# 执行添加
def goodsinsert(request):

    # 判断是否上传了商品图片
    if not request.FILES.get('pic',None):
        return HttpResponse('<script>alert("必须有商品图片");location.href="/myadmin/goodsadd"</script>')


    ob = Goods()
    ob.typeid = Types.objects.get(id=request.POST['typeid'])
    ob.title = request.POST['title']
    ob.price = request.POST['price']
    ob.storage = request.POST['storage']
    ob.pic = upload(request)
    ob.status = request.POST['status']
    ob.save()
    
    return HttpResponse('<script>alert("商品添加成功");location.href="/myadmin/goodslist"</script>')

def goodslist(request):
    v = request.GET.get('keywords','')

    arr = {
        1:['新品','新'],
        2:['热销','热'],
        3:['下架','下']
    }
    status=0
    for k,x in arr.items():
        if v in x:
            status = k


    # Q对象
    from django.db.models import Q

    ob = Goods.objects.filter(Q(id__contains=v)|Q(title__contains=v)|Q(typeid__name__contains=v)|Q(price__contains=v)|Q(status__contains=status))
    # 实例化分页类,一页有两条数据
    paginator = Paginator(ob,3)
     # 浏览器路径？后面参数,获取当前页码
    p = int(request.GET.get('p',1))
     # 获取当前页的所有数据
    userinfo= paginator.page(p)
  
    # 分配数据
    context = {'users':userinfo,'p':p}
    # 加载模板
    return render(request,'myadmin/goods/goodslist.html',context)

def goodsdel(request,uid):
    ob = Goods.objects.get(id = uid)
    os.remove('.'+ob.pic)
    ob.delete()
    return HttpResponse('<script>alert("删除成功");location.href="/myadmin/goodslist"</script>') 

def goodsedit(request,uid):
    obs = typetall()
    # 查询当前的分类信息
    ob = Goods.objects.get(id = uid)
    # 分配数据
    context = {'tinfo':ob,'typelist':obs}
    return render(request,'myadmin/goods/goodsedit.html',context)

def goodsupdate(request):
    try:
        ob = Goods.objects.get(id=request.POST['tid'])
        ob.title = request.POST['title']
        ob.price = request.POST['price']
        ob.storage = request.POST['storage']
        ob.info = request.POST['content']
        if request.FILES.get('pic',None):
                os.remove('.'+ob.pic) 
                ob.pic = upload(request)
        ob.status = request.POST['status']
        ob.save()
        return HttpResponse('<script>alert("商品修改成功");location.href="/myadmin/goodslist"</script>')
    except:
        return HttpResponse('<script>alert("修改失败");location.href="/myadmin/goodsedit/"</script>')

# 上传图片
def upload(request):

    #执行图片的上传
    myfile = request.FILES.get("pic",None)

    filename = str(time.time())+"."+myfile.name.split('.').pop()
    destination = open("./static/public/pics/"+filename,"wb+")
    for chunk in myfile.chunks():      # 分块写入文件  
        destination.write(chunk)  
    destination.close()
    return '/static/public/pics/'+filename